<!-- 商品信息修改申请记录 -->
<template>
  <el-card>
    <div class="table">

      <!-- 表格 -->
      <el-table v-loading="loading" :data="modifyRequestList" style="width: 100%">

        <el-table-column v-for="item in launchRequestTableLabel" :key="item.prop" :label="item.label" :prop="item.prop"
          :width="item.width" :sortable="item.sortable" />
        <!-- 商品类别 -->
        <el-table-column label="商品类别">
          <template v-slot="scope">
            <el-tag v-if="currentCategoryList.length > scope.$index" type="info">
              {{ currentCategoryList[scope.$index].categoryName }}
            </el-tag>
          </template>
        </el-table-column>
        <!--  申请状态 -->
        <el-table-column :filters="[{ text: '已通过', value: 1 }, { text: '申请中', value: 0 }, { text: '不通过', value: 2 }]"
          prop="status" label="申请状态" :filter-method="filterTag">
          <template v-slot="scope">
            <el-tag size="small" v-if="scope.row.status === 1" type="success">已通过</el-tag>
            <el-tag size="small" v-else-if="scope.row.status === 0" type="warning">申请中</el-tag>
            <el-tag size="small" v-else type="danger">不通过</el-tag>
          </template>
        </el-table-column>
        <!-- 相关操作 -->
        <el-table-column fixed="right" label="操作">
          <template v-slot="scope">
            <el-button type="primary" size="small" @click="getLaunchRequestDetail(scope.$index), dialogVisible = true">详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination background layout="prev, pager, next" :total="config.total" class="pager mt-4"
        @current-change="changePage" :page-size="config.size" />
    </div>
  </el-card>

  <!-- 查看商品上架申请详情的模态框 -->
  <el-dialog v-model="dialogVisible" title="查看上架申请详情" width="50%">

    <!-- 商品详细信息展示表格 -->
    <el-descriptions :column="1" size="default" border>
      <el-descriptions-item min-width="130px" label="商品名称" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.commodityName }}
      </el-descriptions-item>
      <el-descriptions-item label="商品价格" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.price.toFixed(2) }}
      </el-descriptions-item>
      <el-descriptions-item label="商品类别" label-align="center" align="center">
        <el-tag type="info" size="large">{{ currentCategoryList[detailIndex].categoryName }}</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="商品库存" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.stocks }}
      </el-descriptions-item>
      <el-descriptions-item label="商品图片" label-align="center" align="center">
        <el-image v-for="item in modifyRequestDetail.modifyRequest.pictures.map(img_url_pre)"
          style="width: 100px; height: 100px; margin-right: 10px; border-radius: 5px;" @dragstart.prevent :src="item"
          :zoom-rate="1.2" :preview-src-list="modifyRequestDetail.modifyRequest.pictures.map(img_url_pre)" fit="cover" />
      </el-descriptions-item>
      <el-descriptions-item label="商品描述" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.description }}
      </el-descriptions-item>
      <el-descriptions-item label="申请发起时间" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.initiateTime }}
      </el-descriptions-item>
      <el-descriptions-item label="申请审核时间" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.processTime }}
      </el-descriptions-item>
      <el-descriptions-item label="申请状态" label-align="center" align="center">
        <el-tag size="large" v-if="modifyRequestDetail.modifyRequest.status === 1" type="success">已通过</el-tag>
        <el-tag size="large" v-else-if="modifyRequestDetail.modifyRequest.status === 0" type="warning">申请中</el-tag>
        <el-tag size="large" v-else type="danger">不通过</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="审核意见" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.info }}
      </el-descriptions-item>
    </el-descriptions>

    <!-- 底部的按钮 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { getCurrentInstance, onMounted, ref, reactive } from 'vue';
import { useStore } from 'vuex';
import img_url_pre from '../../../utils/img_url_filter';

const store = useStore();
const { proxy } = getCurrentInstance();

let manageStoreId = ref("");
let modifyRequestList = ref([]);
// 和当前launchRequestList中的条目相对应
let currentCategoryList = ref([]);
// 控制表格加载遮罩窗是否出现
let loading = ref(false);
// 控制模态框的显示和隐藏
const dialogVisible = ref(false)
// 控制分页
let config = reactive({
  total: 0,  // 总的条目数
  current: 1,  // 当前页
  size: 15  // 一页的条目数
})

const launchRequestTableLabel = reactive([
  {
    prop: "commodityModifyRequestId",
    label: "商品修改申请ID",
    width: 200,
    sortable: true,
  },
  {
    prop: "commodityName",
    label: "商品名称",
    width: 230
  },
  {
    prop: "initiateTime",
    label: "发起时间",
    sortable: true,
    width: 200,
  },
  {
    prop: "processTime",
    label: "处理时间",
    sortable: true,
    width: 200,
  }
]);

let modifyRequestDetail = reactive({
  modifyRequest: {
    "commodityModifyRequestId": "",
    "initiateTime": "",
    "processTime": "",
    "commodityId": "",
    "commodityName": "",
    "storeId": "",
    "categoryId": "",
    "price": 0.00,
    "pictures": [],
    "description": "",
    "stocks": 0,
    "status": 0,
    "info": ""
  }
});

// 获取商店的商品列表
const getModifyRequestList = async (storeId) => {
  // 发起请求
  let res = await proxy.$api.commodity.getModifyRequestFromStore(storeId, {
    current: config.current,
    size: config.size,
  });
  // 更新表格数据
  modifyRequestList.value = res.data.modifyRequestList
  config.total = res.data.total
  // 遍历并获取商品对应类别名
  currentCategoryList.value = [];
  for (let i = 0; i < modifyRequestList.value.length; ++i) {
    let ret = await proxy.$api.category.getCategory(modifyRequestList.value[i].categoryId);
    currentCategoryList.value.push(ret.data);
  }
};

// 这里直接用索引号找到launchRequestList中的信息即可
const getLaunchRequestDetail = (index) => {
  modifyRequestDetail.modifyRequest = modifyRequestList.value[index];
  detailIndex.value = index;
}

const changePage = async (page) => {
  loading.value = true;
  // 更新当前页码
  config.current = page
  await getModifyRequestList()
  loading.value = false;
}

let filterTag = (value, row) => {
  return row.status === value;
}

// 所有商品类别列表
const categoryList = ref([]);
let detailIndex = ref(0);

onMounted(async () => {
  manageStoreId.value = store.state.store.manageStoreId
  loading.value = true;
  await getModifyRequestList(manageStoreId.value);
  // await getAllCategory();
  loading.value = false;
})
</script>

<style lang="less" scoped>
.table {
  position: relative;
  // height: 520px;

  .pager {
    position: relative;
    bottom: -15px;
    justify-content: flex-end;
    margin-right: 15px;
    margin-top: 10px;
    margin-bottom: 15px;
  }
}
</style>